package com.turing.usdtpay.mvp.fragment;

import android.content.Intent;
import android.os.Bundle;
import android.text.TextUtils;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Button;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.turing.usdtpay.R;
import com.turing.usdtpay.base.BaseFragment;
import com.turing.usdtpay.common.SPKeyConstants;
import com.turing.usdtpay.entity.AccountData;
import com.turing.usdtpay.entity.LoginData;
import com.turing.usdtpay.entity.PayData;
import com.turing.usdtpay.manager.UserManager;
import com.turing.usdtpay.mvp.activity.AccountActivity;
import com.turing.usdtpay.mvp.activity.AuthActivity;
import com.turing.usdtpay.mvp.activity.ChangePwdActivity;
import com.turing.usdtpay.mvp.activity.InviteActivity;
import com.turing.usdtpay.mvp.activity.LoginActivity;
import com.turing.usdtpay.mvp.activity.NickNameActivity;
import com.turing.usdtpay.mvp.activity.PayActivity;
import com.turing.usdtpay.mvp.activity.PhoneActivity;
import com.turing.usdtpay.mvp.contract.AccountContract;
import com.turing.usdtpay.mvp.presenter.AccountPresenter;
import com.turing.usdtpay.service.PushService;
import com.turing.usdtpay.utils.LogUtils;
import com.turing.usdtpay.utils.SPHelper;
import com.turing.usdtpay.utils.ToastUtils;

import javax.xml.transform.Result;

/**
 * @作者：JAY
 * @创建时间：2019/10/16 14:50
 * @描述：
 **/
public class AccountFragment extends BaseFragment implements AccountContract.View {
    private String TAG = "AccountFragment";
    private TextView mName,mUid,mAuth;
    private LinearLayout mPwdLayout,mPayLayout,mNameLayout,mPhoneLayout,mInviteLayout;
    private Button mExit;
    private AccountContract.Present mPresent;
    private String mNickName,mPhone;
    private PayData mData;
    public static AccountFragment newInstance(Bundle bundle){
        AccountFragment fragment = new AccountFragment();
        if (bundle != null){
            fragment.setArguments(bundle);
        }
        return fragment;
    }

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setPresenter(new AccountPresenter(this));
    }

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
        View view = inflater.inflate(R.layout.fragment_account, container, false);
        initView(view);
        initTitle();
        initData();
        return view;
    }


    private void initData() {
        LoginData loginDate = UserManager.getInstance().getLoginDate();
        if (loginDate != null){
            mName.setText(loginDate.getAccount());
            mUid.setText("UID :"+loginDate.getTrader());
        }
        mPresent.getUserInfo(getActivity());
    }

    private void initTitle() {
        setTitle("我的");
        showBackBtn(false);
        showTitleLogo(false);
    }

    private void initView(View view) {
        mName = (TextView) view.findViewById(R.id.tv_name);
        mUid = (TextView) view.findViewById(R.id.tv_uid);
        mAuth = (TextView) view.findViewById(R.id.tv_auth);
        mPwdLayout = (LinearLayout) view.findViewById(R.id.pwd_layout);
        mPayLayout = (LinearLayout) view.findViewById(R.id.pay_layout);
        mNameLayout = (LinearLayout) view.findViewById(R.id.name_layout);
        mPhoneLayout = (LinearLayout) view.findViewById(R.id.phone_layout);
        mInviteLayout = (LinearLayout) view.findViewById(R.id.invite_layout);
        mExit = (Button) view.findViewById(R.id.btn_exit);
        mAuth.setOnClickListener(this);
        mPwdLayout.setOnClickListener(this);
        mPayLayout.setOnClickListener(this);
        mNameLayout.setOnClickListener(this);
        mPhoneLayout.setOnClickListener(this);
        mInviteLayout.setOnClickListener(this);
        mExit.setOnClickListener(this);
    }

    @Override
    public void onClick(View v) {
        if (v.getId() == mAuth.getId()){
            getActivity().startActivity(new Intent(getActivity(),AuthActivity.class));
        }else if (v.getId() == mPwdLayout.getId()){
            Intent intent = new Intent(getActivity(), ChangePwdActivity.class);
            startActivityForResult(intent,1000);
        }else if (v.getId() == mPayLayout.getId()){
            Intent intent = new Intent(getActivity(), PayActivity.class);
            intent.putExtra("data",mData);
            startActivityForResult(intent,2000);
        }else if (v.getId() == mNameLayout.getId()){
            Intent intent = new Intent(getActivity(),NickNameActivity.class);
            intent.putExtra("name",mNickName);
            startActivityForResult(intent,3000);
        }else if (v.getId() == mPhoneLayout.getId()){
            Intent intent = new Intent(getActivity(),PhoneActivity.class);
            intent.putExtra("tel",mPhone);
            startActivityForResult(intent,4000);
        }else if (v.getId() == mInviteLayout.getId()){
            getActivity().startActivity(new Intent(getActivity(),InviteActivity.class));
        }else if (v.getId() == mExit.getId()){
            Intent intent = new Intent(getActivity(),PushService.class);
            getActivity().stopService(intent);
            getActivity().startActivity(new Intent(getActivity(),LoginActivity.class));
            SPHelper helper = new SPHelper(SPKeyConstants.TR_SDK_CONFIG);
            helper.remove(SPKeyConstants.TR_SDK_LATE_ACCOUNT);
            getActivity().finish();
        }
    }


    @Override
    public void loginOutSuccess() {
        getActivity().startActivity(new Intent(getActivity(),LoginActivity.class));
        SPHelper helper = new SPHelper(SPKeyConstants.TR_SDK_CONFIG);
        helper.remove(SPKeyConstants.TR_SDK_LATE_ACCOUNT);
        getActivity().finish();
    }

    @Override
    public void onSuccess(AccountData data) {
        if (data.getCheck_status() == 0){
            mAuth.setText("未认证");
        }else if (data.getCheck_status() == 1){
            mAuth.setText("已认证");
        }else if (data.getCheck_status() == 2){
            mAuth.setText("待审核");
        }else if (data.getCheck_status() == 3){
            mAuth.setText("认证失败");
        }
        mNickName = data.getNickname();
        mPresent.getTel(getActivity());
        mPresent.getPayInfo(getActivity());
    }

    @Override
    public void getTel(String tel) {
        mPhone = tel;
    }

    @Override
    public void onFail(String msg) {
        ToastUtils.showNormolToast(getActivity(),msg);
    }

    @Override
    public void getPayInfoSuccess(PayData data) {
        LogUtils.d(TAG+"GET PayInfo -->"+data.toString());
        mData = data;
    }

    @Override
    public void setPresenter(AccountContract.Present presenter) {
        mPresent = presenter;
    }

    @Override
    public void onActivityResult(int requestCode, int resultCode, Intent data) {
        LogUtils.d(TAG,"request Code ->"+requestCode +",resultCode ->"+resultCode);
        if (requestCode == 1000 && resultCode == 1){
            getActivity().startActivity(new Intent(getActivity(),LoginActivity.class));
            getActivity().finish();
        }else if (requestCode == 2000 && resultCode == 2){
            mPresent.getPayInfo(getActivity());
        }if (requestCode == 3000 && resultCode == 3){
            mNickName = data.getStringExtra("name");
        }else if (requestCode == 4000 && resultCode == 4){
            mPhone = data.getStringExtra("tel");
        }
        super.onActivityResult(requestCode, resultCode, data);

    }
}
